package com.kangfawei.shiro.controller;

import com.kangfawei.shiro.service.UserService;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;

@Controller
public class LoginController {
    @Resource
    private UserService userService;

    @GetMapping("/login")
    public String login(){
        return "/login";
    }

    @PostMapping("/login")
    public String login(String username,String password,Model model){

        Integer status = userService.login(username,password);
        if (status.equals(1)){
            return "redirect:/index";
        }
        if(status.equals(2)){
            model.addAttribute("msg","不存在该用户名！");
        }
        if (status.equals(3)){
            model.addAttribute("msg","账号或密码错误！");
        }
        if (status.equals(4)){
            model.addAttribute("msg","登录异常！");
        }
        return "/login";
    }

    @GetMapping(value = {"","/index"})
    public String index(){
        return "/index";
    }
}
